﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using He.Common;
using System.Data.SQLite;
namespace He.DAL
{
    /// <summary>
    /// 数据访问类:Member
    /// </summary>
    public partial class member
    {
        public readonly string TblName = SysKeys.TABLE_PREFIX + "member";

        #region  Method

        /// <summary>
        /// 检查用户名是否重复
        /// </summary>
        /// <param name="user_name"></param>
        /// <returns></returns>
        public bool Exists(string user_name)
        {
            string strSql = "select count(1) from " + TblName + " where user_name=@user_name";
            SQLiteParameter[] parameters = {
                new SQLiteParameter("@user_name",DbType.String)};
            parameters[0].Value = user_name;
            return DbHelperSQLite.Exists(strSql, parameters);
        }

        /// <summary>
        /// 是否存在会员
        /// </summary>
        public bool ExistsMember(int rank_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where rank_id=@rank_id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@rank_id", DbType.Int32,4)
			};
            parameters[0].Value = rank_id;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 检查qq_openid是否存在
        /// </summary>
        /// <param name="qq_openid"></param>
        /// <returns></returns>
        public bool ExistsQQOpenid(string qq_openid)
        {
            string strSql = "select count(1) from " + TblName + " where qq_openid=@qq_openid";
            SQLiteParameter[] parameters = {
                new SQLiteParameter("@qq_openid",DbType.String)};
            parameters[0].Value = qq_openid;
            return DbHelperSQLite.Exists(strSql, parameters);
        }
        /// <summary>
        /// 是否存在Email
        /// </summary>
        public bool ExistsEmail(string email)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where email=@email");
            SQLiteParameter[] parameters = {
			    new SQLiteParameter("@email", DbType.String)
			};
            parameters[0].Value = email;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在Email
        /// </summary>
        public bool ExistsEmail(string username, string email)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where user_name=@user_name and email=@email");
            SQLiteParameter[] parameters = {
			    new SQLiteParameter("@user_name", DbType.String),
                new SQLiteParameter("@email", DbType.String)
			};
            parameters[0].Value = username;
            parameters[1].Value = email;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string user_name, string user_pwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where user_name=@user_name and user_pwd=@user_pwd");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@user_name", DbType.String),
                    new SQLiteParameter("@user_pwd", DbType.String)
            };
            parameters[0].Value = user_name;
            parameters[1].Value = user_pwd;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public He.Model.member GetModel(string user_name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + TblName);
            strSql.Append(" where user_name=@user_name");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@user_name", DbType.String)
};
            parameters[0].Value = user_name;

            He.Model.member model = new Model.member();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public He.Model.member GetModelByOpenid(string openid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from he_member ");
            strSql.Append(" where qq_openid=@qq_openid");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@qq_openid",DbType.String)
};
            parameters[0].Value = openid;

            He.Model.member model = new Model.member();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string orderFiled)
        {
            return DbHelperSQLite.GetPageList(TblName, pageSize, currentPage, "*", strWhere, orderFiled);
        }

        #endregion  Method
    }
}

